//
//  DetailsViewController.m
//  片刻
//
//  Created by lanou3g on 15/6/5.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DetailsViewController.h"
#import "MaocHeader.h"
#import "RadioModel.h"
#import "DetailsTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "PlayViewController.h"
@interface DetailsViewController ()
@property (nonatomic,retain) UITableView *detalis;
@property (nonatomic,retain) NSMutableArray *radioArr;
@property (nonatomic,retain) RadioModel *mode;

@property (nonatomic,retain) UIImageView *backimage; //图片
@property (nonatomic,retain) UILabel *shuoLabel; //作者说说
@property (nonatomic,retain) UILabel *vistLabel; //访问人数
@property (nonatomic,retain) UIImageView *uerimege; //作者头像
@property (nonatomic,retain) UILabel *nameLabe; //作者名字
@property (nonatomic,retain) UIImageView *imageVist; //图标

@end

@implementation DetailsViewController
-(void)dealloc
{
    self.detalis = nil;
    self.radioArr = nil;
    self.mode = nil;
    self.backimage = nil;
    self.shuoLabel = nil;
    self.vistLabel = nil;
    self.uerimege = nil;
    self.nameLabe = nil;
    self.nameLabel = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.radioArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self createData];
    [self createRadioTanelView];
   
    
}


-(void)createRadioTanelView
{
    _detalis = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HiGHT - 70) style:(UITableViewStylePlain)];
    _detalis.delegate = self;
    _detalis.dataSource = self;
    [_detalis registerClass:[DetailsTableViewCell class] forCellReuseIdentifier:@"details"];
    [self.view addSubview:_detalis];
//    self.naviLabel.text =[[RadioModel alloc] init].;
    [_detalis release];
}

//设置Header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    view1.backgroundColor = [UIColor whiteColor];
//    作者名字
     _nameLabe = [[UILabel alloc] initWithFrame:CGRectMake(35, 162, 70, 20)];
    _nameLabe.font = [UIFont systemFontOfSize:13];
    NSDictionary *dic = _mode.userinfo;
    _nameLabe.text = [NSString stringWithFormat:@"%@",dic[@"uname"]];
    [view1 addSubview:_nameLabe];
//    大图片
   _backimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    [_backimage sd_setImageWithURL:[NSURL URLWithString:_mode.coverimg]];
    [view1 addSubview:_backimage];
    _detalis.tableHeaderView = view1;
//  作者头像
    _uerimege = [[UIImageView alloc] initWithFrame:CGRectMake(10, 160, 20, 20)];
    [_uerimege sd_setImageWithURL:[NSURL URLWithString:dic[@"icon"]]];
    _uerimege.layer.cornerRadius = 10; //边框变圆
    _uerimege.layer.masksToBounds = YES;
    [view1 addSubview:_uerimege];
//    作者说说
    _shuoLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 190, SCREEN_WIDTH - 20, 20)];
    _shuoLabel.font = [UIFont systemFontOfSize:13];
    [_shuoLabel setTextColor:[UIColor grayColor]];
    _shuoLabel.text = _mode.desc;
    [view1 addSubview:_shuoLabel];
//    电台访问人数
    _vistLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 80, 160, 70, 20)];
    _vistLabel.font = [UIFont systemFontOfSize:13];
    _vistLabel.text = [NSString stringWithFormat:@"%@",_mode.musicvisitnum];
    [view1 addSubview:_vistLabel];
//    图标
    self.imageVist = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wifi"]];
    _imageVist.frame = CGRectMake(SCREEN_WIDTH - 100, 163, 15, 15);
    [view1 addSubview:_imageVist];
    return view1;
    
}
//设置Header 大小
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 220;
}

-(void)createData
{

    RequesetManager *requeset = [[RequesetManager alloc] initWithRequestURL:@"http://api2.pianke.me/ting/radio_detail" requestModel:POST parameyer:nil];
    requeset.parameyerDict =[@{@"radioid":self.tingID,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"} mutableCopy];
    requeset.delegate = self;
    [requeset startRequest];
    
    [requeset release];

 
}

-(void)handleChangeFramButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mark - ReqursetDelegate
-(void)requestDidFinshWithData:(NSData *)data{
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
      NSDictionary *dict1 = dict[@"data"];
    NSArray *arr = dict1[@"list"];
    for (NSDictionary *dict in arr) {
        RadioModel *mobel = [[RadioModel alloc] init];
        [mobel setValuesForKeysWithDictionary:dict];
        [self.radioArr addObject:mobel];
        [mobel release];
    }

//    最上面的那些信息
    NSDictionary *ditc2 = dict1[@"radioInfo"];
    _mode = [[RadioModel alloc] init];
    [_mode setValuesForKeysWithDictionary:ditc2]; //遍历
    
    [self.detalis reloadData];//刷新
    
}
-(void)requestError:(NSError *)error
{
    NSLog(@"error:%@",error);
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.radioArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    DetailsTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"details" forIndexPath:indexPath];
    RadioModel *model = self.radioArr[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayViewController *play = [[PlayViewController alloc] init];
    _mode = _radioArr[indexPath.row];
    play.tingID = _mode.tingid;
    play.i = indexPath.row;
    play.upArr = [NSMutableArray array];
    for (RadioModel *model in self.radioArr) {
        [play.upArr addObject:model];
    }
    [self.navigationController pushViewController:play animated:YES];
    [play release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
